package com.itboye.sunsun.test;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.android.volley.Request;
import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.itboye.sunsun.constants.NetPublicConstant;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.test.bean.BikeBean;
import com.itboye.sunsun.test.bean.PersonDataBean;
import com.itboye.sunsun.test.bean.WeatherBean;
import com.itboye.sunsun.utils.ParamDeal;
import com.itboye.sunsun.utils.TimesUtils;
import com.itboye.sunsun.volley.XJsonRequest;
import com.itboye.sunsun.volley.XRequestListener;
import com.nostra13.universalimageloader.utils.StorageUtils;

import android.content.Context;
import android.util.Log;

public class VolleyExample {

	
	public static void main(){
		
//		HttpRequest.getDefaultRequestQueue().add(  bike()  );
		
//		HttpRequest.getDefaultRequestQueue().add(  weather()  );
		File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "imageloader/Cache");  
		login();
	}
	
	
	private static Context getApplicationContext() {
		// TODO Auto-generated method stub
		return null;
	}


	public static void login()
	{
		String url="http://rjy.itboye.com/api.php?access_token=by565fa4facdb191";
		String time = TimesUtils.getStamp();
		ParamDeal operate = new ParamDeal();
		Map<String,String> map=new HashMap<>();
		map.put("username", "15958043299");
		map.put("password", "123456789");
		String data = operate.dataEncrypt(map);
		String sign = operate.getMD5Sign(time, "BY_User_login", data,
				NetPublicConstant.Client_Id_Value,
				NetPublicConstant.Notify_Id_Value);
		
		
		XJsonRequest<PersonDataBean>request=new XJsonRequest<>(url, new XRequestListener<PersonDataBean>() {

			@Override
			public void onResponse(PersonDataBean response) {
				Log.v("result", response.toString());
			}
		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				Log.v("volleyerr", error.toString());
			}
		})
		.addParam(NetPublicConstant.Grant_Type_Key,NetPublicConstant.Grant_Type_Value)
		.addParam(NetPublicConstant.Client_Id_Key,NetPublicConstant.Client_Id_Value)
		.addParam(NetPublicConstant.Client_Secret_Key,NetPublicConstant.Client_Secret_Value)

		// 公共参数
		.addParam(NetPublicConstant.Time_Key,time)
		.addParam(NetPublicConstant.Alg_Key,NetPublicConstant.Alg_Value)
		.addParam(NetPublicConstant.API_Ver_Key,NetPublicConstant.API_Ver_Value)
		.addParam(NetPublicConstant.Notify_Id_Key,NetPublicConstant.Notify_Id_Value)
		.addParam(NetPublicConstant.Access_Token_Key,NetPublicConstant.Client_Id_Value)
		.addParam(NetPublicConstant.Sign_Key, sign)
		.addParam(NetPublicConstant.Type_Key, "BY_User_login")
		// 请求数据
		.addParam(NetPublicConstant.Data_Key, data);
		
		
		
		HttpRequest.getDefaultRequestQueue().add(request);
	}
	
	
	private static Request<List<BikeBean>> bike() {

		String url = "http://hztrip.org/api/bike/round?lon=120.371964&lat=30.322133";

		XJsonRequest<List<BikeBean>> request = new XJsonRequest<>(url, new XRequestListener<List<BikeBean>>() {

			@Override
			public void onResponse(List<BikeBean> response) {
				Log.v("result", response.toString());
			}
		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				Log.v("volleyerr", error.toString());
			}
		});

		return request;

	}

	/**
	 * 获取  百度 api  store 天气信息
	 * @return
	 */
	private static Request<WeatherBean> weather() {
		String httpUrl = "http://apis.baidu.com/apistore/weatherservice/citylist";
		String httpArg = "cityname=%E6%9C%9D%E9%98%B3";
		String url = httpUrl + "?" + httpArg;
		XJsonRequest<WeatherBean> request = new XJsonRequest<WeatherBean>(url, new XRequestListener<WeatherBean>() {

			@Override
			public void onResponse(WeatherBean arg0) {
				Log.v("result", arg0.toString());
			}
		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError arg0) {
				Log.v("volleyerr", arg0.toString());

			}
		}).addHeader("apikey", "0ec4ef9237be00e62fcda68c1417a28e")
				.addHeader("testk", "testv")
				.addParam("testk", "testv")
				.setMethod(Method.POST);

		return request;
	}

}
